﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pwblog.Entities;
using System.Xml.Linq;

namespace pwblog.DataAccess
{
    public class ArticleXmlDao
    {
        private string pathXml = @"C:\Users\tangphamtunglong1404\Documents\Visual Studio 2010\Projects\pwblog\pwblog\Web\Articles\XMLFile.xml";

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ArticleXml[] GetArticlesFromXml()
        {
            XDocument xDocument = XDocument.Load(pathXml);
            var articles = from article in xDocument.Descendants("article")
                           select new ArticleXml(
                               article.Element("id").Value,
                               article.Element("title").Value,
                               article.Element("pubDate").Value,
                               article.Element("creator").Value,
                               article.Element("description").Value,
                               article.Element("link").Value);
            return articles.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="articleXml"></param>
        public void InsertArticleXml(ArticleXml articleXml)
        {
            XElement doc = XElement.Load(pathXml);

            XElement newBook = 
                new XElement("article",
                    new XElement("id", articleXml.Id),
                    new XElement("title", articleXml.Title),
                    new XElement("pubDate", articleXml.PubDate),
                    new XElement("creator", articleXml.Creator),
                    new XElement("description", new XCData(articleXml.Description)),
                    new XElement("link", articleXml.Link));

            doc.Add(newBook);
            doc.Save(pathXml);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="articleXml"></param>
        public void UpdateArticleXml(ArticleXml articleXml)
        {
            XElement doc = XElement.Load(pathXml);

            XElement singleArticle = (from b in doc.Elements("article")
                                                   where ((string)b.Element("id")).Equals(articleXml.Id)
                                                   select b).First();
            singleArticle.ReplaceNodes(
                new XElement("id", articleXml.Id),
                    new XElement("title", articleXml.Title),
                    new XElement("pubDate", articleXml.PubDate),
                    new XElement("creator", articleXml.Creator),
                    new XElement("description", new XCData(articleXml.Description)),
                    new XElement("link", articleXml.Link));

            //singleArticle.SetElementValue("title", articleXml.Title);
            //singleArticle.SetElementValue("pubDate", articleXml.PubDate);
            //singleArticle.SetElementValue("creator", articleXml.Creator);
            //singleArticle.SetElementValue("description", new XCData(articleXml.Description));

            doc.Save(pathXml);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteArticleXml(string id)
        {
            XElement doc = XElement.Load(pathXml);

            //obtain a single book
            XElement singleArticle = (from b in doc.Elements("article")
                                      where ((string)b.Element("id")).Equals(id)
                                      select b).First();

            singleArticle.Remove();
            doc.Save(pathXml);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal ArticleXml GetArticleXmlById(string id)
        {
            XElement doc = XElement.Load(pathXml);

            var singleArticle = from article in doc.Elements("article")
                                       where ((string)article.Element("id")).Equals(id)
                                        select new ArticleXml(
                                               article.Element("id").Value,
                                               article.Element("title").Value,
                                               article.Element("pubDate").Value,
                                               article.Element("creator").Value,
                                               article.Element("description").Value,
                                               article.Element("link").Value);

            return singleArticle.First();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal ArticleXml[] GetArticlesByKey(string key)
        {   
            XElement doc = XElement.Load(pathXml);

            var articles = (from article in doc.Elements("article")
                            where ((string)article.Element("title")).Contains(key)
                            select new ArticleXml(
                                 article.Element("id").Value,
                                 article.Element("title").Value,
                                 article.Element("pubDate").Value,
                                 article.Element("creator").Value,
                                 article.Element("description").Value,
                                 article.Element("link").Value));

            return articles.ToArray();
        }
    }
}